package com.ekuaitu.kuaitustaff.utils.ble;

import com.ekuaitu.kuaitustaff.utils.Logger;
import com.ekuaitu.kuaitustaff.utils.ble.bean.CarRequireStatus;
import com.ekuaitu.kuaitustaff.utils.ble.bean.CommandBleDbBean;
import com.ekuaitu.kuaitustaff.utils.ble.bean.ZisCarStatusBean;
import com.ekuaitu.kuaitustaff.utils.ble.bean.ZisMileRangeBean;
import com.ekuaitu.kuaitustaff.utils.ble.utils.HexUtil;

import java.util.Arrays;

/**
 * Created by michaelpi on 2018/5/31.
 */

public class ZXTParseUtils {

    private static final ZXTParseUtils instance = new ZXTParseUtils();

    public static ZXTParseUtils getInstance() {
        return instance;
    }

    public int[] status = new int[16];

    String parseControlResult(int result, String sendCode) {
        String error = null;
        switch (result) {
            case 0x00:// 成功
                Logger.i("bleinfo", "response control ok");
                break;
            case 0x01:
                error = "响应-设备回复忙";
                break;
            case 0x02:
                error = "响应-设备不支持当前指令";
                break;
            case 0x04:// 收到控制时间超过有效期(10分钟)
                error = "响应-密码失效";
                break;
            case 0x06:
                error = "响应-无效授权";
                break;
            case 0x19:
                error = "响应-发动机未熄火";
                break;
            case 0x1A:// 原车PKE操作退出执行（仅开门有）
                error = "响应-车辆已被原车接管，请拔掉钥匙重试！";
                break;
            case 0x1B:
                error = "响应-车门未关";
                break;
            case 0x1C:// 动作执行前执行失败（开门、锁门、寻车都有，出现此问题基本
                error = "响应-控制失败";
                break;
            case 0x1D:
                error = "响应-车门未关";
                break;
            case 0x1E:// 开锁后中控锁为锁状态 (开锁门不成功)
                error = "响应-控制失败";
                break;
            case 0x38:
                error = "响应-车灯或ACC未关";
                break;
            default:
                error = "响应-控制失败";
                break;
        }
        return error;
    }


    void parseCarStatus(CommandBleDbBean bleDbBean) {
        // 状态
        try {
            ZisCarStatusBean zisCarStatusBean = ZisCarStatusBean.CreateBean(bleDbBean.getIdc(), System.currentTimeMillis(), HexUtil.hexStringToByteArray(bleDbBean.getData()), 8);
            if (zisCarStatusBean != null) {
                CarRequireStatus carRequireStatus = ZisCarStatusBean.AnalyticStatusNew(zisCarStatusBean.LightState, zisCarStatusBean.DoorState, zisCarStatusBean.OtherState);
                status = new int[16];
                status[BleConstants.CarStatus.FL_DOOR] = carRequireStatus.frontLeft;
                status[BleConstants.CarStatus.FR_DOOR] = carRequireStatus.frontRight;
                status[BleConstants.CarStatus.RL_DOOR] = carRequireStatus.rearLeft;
                status[BleConstants.CarStatus.RR_DOOR] = carRequireStatus.rearRight;
                status[BleConstants.CarStatus.TRUNK] = carRequireStatus.boot;
                status[BleConstants.CarStatus.ENGINE] = carRequireStatus.engine;
                status[BleConstants.CarStatus.LIGHT] = carRequireStatus.dippedHeadlight;
                Logger.i("bleinfo-Helper", "ZXT status" + Arrays.toString(status));

            }
        } catch (Exception e) {
            Logger.i("bleInfo-Helper", "parse status err");

            e.printStackTrace();
        }
    }


    int[] parseCarMileage(CommandBleDbBean bleDbBean) {
        // 电量、里程等
        ZisMileRangeBean zisMileRangeBean = ZisMileRangeBean.CreateBean(bleDbBean.getIdc(), System.currentTimeMillis(), HexUtil.hexStringToByteArray(bleDbBean.getData()), 8);
        status[BleConstants.CarStatus.MILEAGE] = Integer.parseInt(zisMileRangeBean.TotalMileage);
        return status;
    }
}
